package managers.conversationManager;

import java.io.IOException;
import java.net.ServerSocket;

import utils.StackTracePrinter;

public class TCPConversationProtocol implements ConversationProtocol {

	private ServerSocket socket;
	private ServerSocket fileTransferSocket;

	public TCPConversationProtocol () throws Exception {
		this(0, 0);
	}
	
	public TCPConversationProtocol (int port, int fileTransferPort) throws Exception {
		socket = new ServerSocket(port);
		fileTransferSocket = new ServerSocket(fileTransferPort);
	}



	public GenericSocket acceptConversationRequest () throws Exception {
		System.out.println("listening");
		return new DefaultTCPSocket(socket.accept());
	}

	public GenericSocket acceptFileTransferRequest () throws Exception {
		return new DefaultTCPSocket(fileTransferSocket.accept());
	}


	public int getConversationPort () {
		return socket.getLocalPort();
	}

	public int getFileTransferPort () {
		return fileTransferSocket.getLocalPort();
	}



	public void close() {
		try {
			socket.close();
			fileTransferSocket.close();
		} catch (IOException e) {
			// do nothing
			StackTracePrinter.print(e);
		}

	}
}
